package com.itheima.main;

import com.itheima.student.Student;
import com.itheima.utils.Utils;

import java.util.ArrayList;
import java.util.Scanner;

public class MainApp {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> stuList = new ArrayList<>();
        while (true) {
            System.out.println("************************************************");
            System.out.println("*   1.学员信息管理   2.教师信息管理   3.退出   *");
            System.out.println("************************************************");
            String op = sc.next();
            switch (op) {
                case "1":
                    studentManager(sc,stuList);
                    break;
                case "2":
                    teacherManager(sc,stuList);
                    break;
                case "3":
                    System.out.println("拜拜了您内！");
                    System.exit(0);
                default:
                    System.out.println("有这选项吗？你就选？赶紧重选！");
                    break;

            }
        }


    }


    private static void teacherManager(Scanner sc, ArrayList<Student> stuList) {
        while (true){
            System.out.println("【教师信息管理】");
            System.out.println("============================================================");
            System.out.println("1.添加教师  2.修改教师  3.删除教师  4.查询教师  5.返回上一级");
            System.out.println("============================================================");
            String op = sc.next();
            switch (op){
                case "1":
                    addTeacher(sc,stuList);
                    break;
                case "2":
                    updateTeacher(sc,stuList);
                    break;
                case "3":
                    deleteTeacher(sc,stuList);

                case "4":
                    findAllTeacher(stuList);

                    break;
                case "5":
                    return;//结束此方法
                default:
                    System.out.println("一共就五个选项你输"+op+"你是**吗！？");
                    break;
            }
        }
    }

    private static void findAllTeacher(ArrayList<Student> stuList) {

    }

    private static void deleteTeacher(Scanner sc, ArrayList<Student> stuList) {

    }

    private static void updateTeacher(Scanner sc, ArrayList<Student> stuList) {

    }

    private static void addTeacher(Scanner sc, ArrayList<Student> stuList) {
        System.out.println("请输入学员信息：");
        System.out.println("姓名：");
        String name = sc.next();
        System.out.println("性别：");
        String sex = sc.next();
        System.out.println("出生日期：");
        String birthday = sc.next();
        //创建Student对象
        Student stu = new Student();
        stu.setId(++Utils.teaId);
        stu.setName(name);
        stu.setSex(sex);
        stu.setBirthday(birthday);
        //将对象储存到集合
        stuList.add(stu);
        //提示成功
        System.out.println("【成功】数据已被保存！");
    }

    private static void studentManager(Scanner sc, ArrayList<Student> stuList) {
        while (true){
            System.out.println("【学员信息管理】");
            System.out.println("============================================================");
            System.out.println("1.添加学员  2.修改学员  3.删除学员  4.查询学员  5.返回上一级");
            System.out.println("============================================================");
            String op = sc.next();
            switch (op){
                case "1":
                    addStudent(sc,stuList);
                    break;
                case "2":
                    updateStudent(sc,stuList);
                    break;
                case "3":
                    deleteStudent(sc,stuList);

                case "4":
                    findAllStudent(stuList);

                    break;
                case "5":
                    return;//结束此方法
                default:
                    System.out.println("一共就五个选项你输"+op+"你是**吗！？");
                    break;
            }
        }
    }

    private static void addStudent(Scanner sc, ArrayList<Student> stuList) {
        System.out.println("请输入学员信息：");
        System.out.println("姓名：");
        String name = sc.next();
        System.out.println("性别：");
        String sex = sc.next();
        System.out.println("出生日期：");
        String birthday = sc.next();
        //创建Student对象
        Student stu = new Student();
        stu.setId(++Utils.stuId);
        stu.setName(name);
        stu.setSex(sex);
        stu.setBirthday(birthday);
        //将对象储存到集合
        stuList.add(stu);
        //提示成功
        System.out.println("【成功】数据已被保存！");
    }

    private static void updateStudent(Scanner sc, ArrayList<Student> stuList) {
        System.out.println("请输入要修改的学员ID：");
        int id = sc.nextInt();
        for (int i = 0; i <stuList.size() ; i++) {
            Student stu = stuList.get(i);
            if (stu.getId() == id){
                Utils.printPerson(stu);
                System.out.println("请输入新姓名[保留原值输入0]：");
                String newName = sc.next();
                System.out.println("请输入新姓别[保留原值输入0]：");
                String newSex = sc.next();
                System.out.println("请输入新生日[保留原值输入0]：");
                String newBirthday = sc.next();
                if (!"0".equals(newName)){
                    stu.setName(newName);
                }
                if (!"0".equals(newSex)){
                    stu.setSex(newSex);
                }
                if (!"0".equals(newBirthday)){
                    stu.setBirthday(newBirthday);
                }
                System.out.println("【成功】学员信息修改成功！");
                return;
            }
        }
        System.out.println("【错误】学员ID:"+id+"没找到!");

    }

    private static void deleteStudent(Scanner sc, ArrayList<Student> stuList) {
        System.out.println("请输入要删除的学员ID：");
        int id = sc.nextInt();
        for (int i = 0; i <stuList.size() ; i++) {
            Student stu = stuList.get(i);
            if (stu.getId()== id){
                System.out.println("【查询结果】要删除的学员信息：");
                Utils.printPerson(stu);
                System.out.println("【确认】您确定要删除这条信息吗（y/n）?");
                String s = sc.next();
                if ("y".equals(s)){
                    stuList.remove(i);
                    System.out.println("【成功】数据已删除！");
                    return;
                }else {
                    System.out.println("【取消】操作被取消！");
                    return;
                }
            }
        }
        System.out.println("【错误】学员ID:"+id+"没找到!");


    }

    private static void findAllStudent(ArrayList<Student> stuList) {
        Utils.printPersonList(stuList);
    }
}
